๊ฐ๋ ฅํ๊ณ ์ ์ง๋ณด์ ๊ฐ๋ฅํ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ํด ์ธํฐํ์ด์ค ์ํคํ ์ฒ, ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ, ์ค๋ฅ ์ฒ๋ฆฌ์ ์ค์ ์ ๋ TypeScript๋ฅผ ์ฌ์ฉํ ํ์ ์์ ํ API ์ค๊ณ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ํ์ํฉ๋๋ค.
TypeScript API ์ค๊ณ: ํ์ ์์ ํ ์ธํฐํ์ด์ค ์ํคํ ์ฒ ๊ตฌ์ถ
ํ๋ ์ํํธ์จ์ด ๊ฐ๋ฐ์์ API(Application Programming Interfaces)๋ ๋ค์ํ ์์คํ ๋ฐ ์๋น์ค ๊ฐ ํต์ ์ ์ค์ถ์ ๋๋ค. ํนํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณต์ก์ฑ์ด ์ฆ๊ฐํจ์ ๋ฐ๋ผ ์ด๋ฌํ API์ ์์ ์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๋ณด์ฅํ๋ ๊ฒ์ด ๋ฌด์๋ณด๋ค ์ค์ํฉ๋๋ค. ๊ฐ๋ ฅํ ํ์ดํ ๊ธฐ๋ฅ์ ๊ฐ์ถ TypeScript๋ ํ์ ์์ ํ API๋ฅผ ์ค๊ณํ๊ณ ๋ฐํ์ ์ค๋ฅ๋ฅผ ์ค์ด๋ฉฐ ๊ฐ๋ฐ์ ์์ฐ์ฑ์ ํฅ์์ํค๋ ๊ฐ๋ ฅํ ๋๊ตฌ ์ธํธ๋ฅผ ์ ๊ณตํฉ๋๋ค.
ํ์ ์์ ํ API ์ค๊ณ๋ ๋ฌด์์ธ๊ฐ์?
ํ์ ์์ ํ API ์ค๊ณ๋ ๊ฐ๋ฐ ํ๋ก์ธ์ค ์ด๊ธฐ์ ์ค๋ฅ๋ฅผ ํฌ์ฐฉํ๊ธฐ ์ํด ์ ์ ํ์ดํ์ ํ์ฉํ๋ ๋ฐ ์ค์ ์ ๋ก๋๋ค. ๋ช ํํ ์ธํฐํ์ด์ค์ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ ์ํจ์ผ๋ก์จ, API๋ฅผ ํตํด ํ๋ฅด๋ ๋ฐ์ดํฐ๊ฐ ์ฌ์ ์ ์๋ ๊ณ์ฝ์ ์ค์ํ๋๋ก ๋ณด์ฅํ ์ ์์ต๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ์์์น ๋ชปํ ๋์์ ์ต์ํํ๊ณ ๋๋ฒ๊น ์ ๋จ์ํํ๋ฉฐ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ๋ฐ์ ์ธ ๊ฒฌ๊ณ ์ฑ์ ํฅ์์ํต๋๋ค.
ํ์ ์์ ํ API๋ ์ ์ก๋๋ ๋ชจ๋ ๋ฐ์ดํฐ ์กฐ๊ฐ์ด ์ ์๋ ํ์ ๊ณผ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค๋ ์์น์ ๊ธฐ๋ฐ์ผ๋ก ๊ตฌ์ถ๋ฉ๋๋ค. ์ด๋ ๋น์ฉ์ด ๋ง์ด ๋ค๊ณ ๋๋ฒ๊น ํ๊ธฐ ์ด๋ ค์ด ๋ฐํ์ ๊ฒ์ฌ์ ์์กดํ๋ ๋์ , ์ปดํ์ผ๋ฌ๊ฐ ์ปดํ์ผ ํ์์ ์ฝ๋์ ์ ํ์ฑ์ ๊ฒ์ฆํ ์ ์๋๋ก ํฉ๋๋ค.
TypeScript๋ฅผ ์ฌ์ฉํ ํ์ ์์ ํ API ์ค๊ณ์ ์ด์
- ๋ฐํ์ ์ค๋ฅ ๊ฐ์: TypeScript์ ํ์ ์์คํ ์ ๊ฐ๋ฐ ์ค์ ๋ง์ ์ค๋ฅ๋ฅผ ํฌ์ฐฉํ์ฌ ํ๋ก๋์ ์ ๋๋ฌํ๋ ๊ฒ์ ๋ฐฉ์งํฉ๋๋ค.
- ์ฝ๋ ์ ์ง๋ณด์์ฑ ํฅ์: ๋ช ํํ ํ์ ์ ์๋ ์ฝ๋๋ฅผ ์ดํดํ๊ณ ์์ ํ๊ธฐ ์ฝ๊ฒ ๋ง๋ค์ด ๋ฆฌํฉํ ๋ง ์ค ๋ฒ๊ทธ ๋ฐ์ ์ํ์ ์ค์ ๋๋ค.
- ๊ฐ๋ฐ์ ์์ฐ์ฑ ํฅ์: IDE์ ์๋ ์์ฑ ๋ฐ ํ์ ๊ฒ์ฌ๋ ๊ฐ๋ฐ ์๋๋ฅผ ํฌ๊ฒ ๋์ด๊ณ ๋๋ฒ๊น ์๊ฐ์ ์ค์ ๋๋ค.
- ํฅ์๋ ํ์ : ๋ช ์์ ์ธ ํ์ ๊ณ์ฝ์ ์์คํ ์ ๋ค๋ฅธ ๋ถ๋ถ์์ ์์ ํ๋ ๊ฐ๋ฐ์ ๊ฐ์ ์์ฌ์ํต์ ์ฉ์ดํ๊ฒ ํฉ๋๋ค.
- ์ฝ๋ ํ์ง์ ๋ํ ์ ๋ขฐ ์ฆ๊ฐ: ํ์ ์์ ์ฑ์ ์ฝ๋๊ฐ ์์๋๋ก ์๋ํ๋ค๋ ํ์ ์ ์ ๊ณตํ์ฌ ์๊ธฐ์น ์์ ๋ฐํ์ ์คํจ์ ๋ํ ๋๋ ค์์ ์ค์ ๋๋ค.
TypeScript์์ ํ์ ์์ ํ API ์ค๊ณ์ ํต์ฌ ์์น
ํจ๊ณผ์ ์ธ ํ์ ์์ ํ API๋ฅผ ์ค๊ณํ๋ ค๋ฉด ๋ค์ ์์น์ ๊ณ ๋ คํ์ญ์์ค.
1. ๋ช ํํ ์ธํฐํ์ด์ค ๋ฐ ํ์ ์ ์
ํ์ ์์ ํ API ์ค๊ณ์ ๊ธฐ๋ฐ์ ๋ช ํํ๊ณ ์ ํํ ์ธํฐํ์ด์ค ๋ฐ ํ์ ์ ์ ์ํ๋ ๊ฒ์ ๋๋ค. ์ด๋ ์์คํ ์ ๋ค๋ฅธ ๊ตฌ์ฑ ์์ ๊ฐ์ ๊ตํ๋๋ ๋ฐ์ดํฐ์ ๊ตฌ์กฐ๋ฅผ ์ง์ํ๋ ๊ณ์ฝ ์ญํ ์ ํฉ๋๋ค.
์์:
interface User {
id: string;
name: string;
email: string;
age?: number; // Optional property
address: {
street: string;
city: string;
country: string;
};
}
type Product = {
productId: string;
productName: string;
price: number;
description?: string;
}
์ด ์์์์๋ User์ ๋ํ ์ธํฐํ์ด์ค์ Product์ ๋ํ ํ์
๋ณ์นญ์ ์ ์ํฉ๋๋ค. ์ด๋ฌํ ์ ์๋ ๊ฐ๊ฐ ์ฌ์ฉ์ ๋ฐ ์ ํ๊ณผ ๊ด๋ จ๋ ๋ฐ์ดํฐ์ ์์ ๊ตฌ์กฐ์ ํ์
์ ์ง์ ํฉ๋๋ค. User ์ธํฐํ์ด์ค์ ์ ํ์ age ์์ฑ์ ์ด ํ๋๊ฐ ํ์๊ฐ ์๋์ ๋ํ๋
๋๋ค.
2. ์ ํ๋ ๊ฐ ์งํฉ์ Enum ์ฌ์ฉ
๊ฐ๋ฅํ ๊ฐ์ ์งํฉ์ด ์ ํ์ ์ธ ๊ฒฝ์ฐ, enum์ ์ฌ์ฉํ์ฌ ํ์ ์์ ์ฑ์ ๊ฐํํ๊ณ ์ฝ๋ ๊ฐ๋ ์ฑ์ ๋์ด์ญ์์ค.
์์:
enum OrderStatus {
PENDING = "pending",
PROCESSING = "processing",
SHIPPED = "shipped",
DELIVERED = "delivered",
CANCELLED = "cancelled",
}
interface Order {
orderId: string;
userId: string;
items: Product[];
status: OrderStatus;
createdAt: Date;
}
์ฌ๊ธฐ์ OrderStatus enum์ ์ฃผ๋ฌธ์ ๊ฐ๋ฅํ ์ํ๋ฅผ ์ ์ํฉ๋๋ค. Order ์ธํฐํ์ด์ค์์ ์ด enum์ ์ฌ์ฉํจ์ผ๋ก์จ, status ํ๋๊ฐ ์ ์๋ ๊ฐ ์ค ํ๋๋ง ๋ ์ ์๋๋ก ๋ณด์ฅํฉ๋๋ค.
3. ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ์ํ ์ ๋ค๋ฆญ ํ์ฉ
์ ๋ค๋ฆญ์ ์ฌ์ฉํ๋ฉด ํ์ ์์ ์ฑ์ ์ ์งํ๋ฉด์ ๋ค์ํ ํ์ ๊ณผ ํจ๊ป ์๋ํ ์ ์๋ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
์์:
interface ApiResponse<T> {
success: boolean;
data?: T;
error?: string;
}
async function getUser(id: string): Promise<ApiResponse<User>> {
// Simulate fetching user data from an API
return new Promise((resolve) => {
setTimeout(() => {
const user: User = {
id: id,
name: "John Doe",
email: "john.doe@example.com",
address: {
street: "123 Main St",
city: "Anytown",
country: "USA"
}
};
resolve({ success: true, data: user });
}, 1000);
});
}
์ด ์์์์ ApiResponse<T>๋ ๋ชจ๋ API ์๋ํฌ์ธํธ์ ์๋ต์ ๋ํ๋ด๋ ๋ฐ ์ฌ์ฉ๋ ์ ์๋ ์ ๋ค๋ฆญ ์ธํฐํ์ด์ค์
๋๋ค. T ํ์
๋งค๊ฐ๋ณ์๋ฅผ ํตํด data ํ๋์ ํ์
์ ์ง์ ํ ์ ์์ต๋๋ค. getUser ํจ์๋ ApiResponse<User>๋ก ํด๊ฒฐ๋๋ Promise๋ฅผ ๋ฐํํ์ฌ ๋ฐํ๋ ๋ฐ์ดํฐ๊ฐ User ์ธํฐํ์ด์ค๋ฅผ ์ค์ํ๋๋ก ๋ณด์ฅํฉ๋๋ค.
4. ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ ๊ตฌํ
๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ๋ API๊ฐ ์์ ํ๋ ๋ฐ์ดํฐ๊ฐ ์ ํจํ๊ณ ์์ ํ์๊ณผ ์ผ์นํ๋์ง ํ์ธํ๋ ๋ฐ ์ค์ํฉ๋๋ค. TypeScript๋ zod ๋๋ yup๊ณผ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ํจ๊ป ์ฌ์ฉํ์ฌ ๊ฐ๋ ฅํ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค.
Zod ์ฌ์ฉ ์์:
import { z } from 'zod';
const UserSchema = z.object({
id: z.string().uuid(),
name: z.string().min(2).max(50),
email: z.string().email(),
age: z.number().min(0).max(150).optional(),
address: z.object({
street: z.string(),
city: z.string(),
country: z.string()
})
});
type User = z.infer<typeof UserSchema>;
function validateUser(data: any): User {
try {
return UserSchema.parse(data);
} catch (error: any) {
console.error("Validation error:", error.errors);
throw new Error("Invalid user data");
}
}
// Example usage
try {
const validUser = validateUser({
id: "a1b2c3d4-e5f6-7890-1234-567890abcdef",
name: "Alice",
email: "alice@example.com",
age: 30,
address: {
street: "456 Oak Ave",
city: "Somewhere",
country: "Canada"
}
});
console.log("Valid user:", validUser);
} catch (error: any) {
console.error("Error creating user:", error.message);
}
try {
const invalidUser = validateUser({
id: "invalid-id",
name: "A",
email: "invalid-email",
age: -5,
address: {
street: "",
city: "",
country: ""
}
});
console.log("Valid user:", invalidUser); // This line will not be reached
} catch (error: any) {
console.error("Error creating user:", error.message);
}
์ด ์์์์๋ Zod๋ฅผ ์ฌ์ฉํ์ฌ User ์ธํฐํ์ด์ค์ ๋ํ ์คํค๋ง๋ฅผ ์ ์ํฉ๋๋ค. UserSchema๋ ์ด๋ฉ์ผ ์ฃผ์์ ํ์๊ณผ ์ด๋ฆ์ ์ต์ ๋ฐ ์ต๋ ๊ธธ์ด์ ๊ฐ์ ๊ฐ ํ๋์ ๋ํ ์ ํจ์ฑ ๊ฒ์ฌ ๊ท์น์ ์ง์ ํฉ๋๋ค. validateUser ํจ์๋ ์คํค๋ง๋ฅผ ์ฌ์ฉํ์ฌ ์
๋ ฅ ๋ฐ์ดํฐ๋ฅผ ํ์ฑํ๊ณ ์ ํจ์ฑ์ ๊ฒ์ฌํฉ๋๋ค. ๋ฐ์ดํฐ๊ฐ ์ ํจํ์ง ์์ผ๋ฉด ์ ํจ์ฑ ๊ฒ์ฌ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
5. ๊ฒฌ๊ณ ํ ์ค๋ฅ ์ฒ๋ฆฌ ๊ตฌํ
์ ์ ํ ์ค๋ฅ ์ฒ๋ฆฌ๋ ํด๋ผ์ด์ธํธ์๊ฒ ์ ์ตํ ํผ๋๋ฐฑ์ ์ ๊ณตํ๊ณ ์ ํ๋ฆฌ์ผ์ด์ ์ถฉ๋์ ๋ฐฉ์งํ๋ ๋ฐ ํ์์ ์ ๋๋ค. ์ฌ์ฉ์ ์ง์ ์ค๋ฅ ํ์ ๊ณผ ์ค๋ฅ ์ฒ๋ฆฌ ๋ฏธ๋ค์จ์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ค๋ฅ๋ฅผ ์ฐ์ํ๊ฒ ์ฒ๋ฆฌํ์ญ์์ค.
์์:
class ApiError extends Error {
constructor(public statusCode: number, public message: string) {
super(message);
this.name = "ApiError";
}
}
async function getUserFromDatabase(id: string): Promise<User> {
// Simulate fetching user data from a database
return new Promise((resolve, reject) => {
setTimeout(() => {
if (id === "nonexistent-user") {
reject(new ApiError(404, "User not found"));
} else {
const user: User = {
id: id,
name: "Jane Smith",
email: "jane.smith@example.com",
address: {
street: "789 Pine Ln",
city: "Hill Valley",
country: "UK"
}
};
resolve(user);
}
}, 500);
});
}
async function handleGetUser(id: string) {
try {
const user = await getUserFromDatabase(id);
console.log("User found:", user);
return { success: true, data: user };
} catch (error: any) {
if (error instanceof ApiError) {
console.error("API Error:", error.statusCode, error.message);
return { success: false, error: error.message };
} else {
console.error("Unexpected error:", error);
return { success: false, error: "Internal server error" };
}
}
}
// Example usage
handleGetUser("123").then(result => console.log(result));
handleGetUser("nonexistent-user").then(result => console.log(result));
์ด ์์์์๋ ๋ด์ฅ Error ํด๋์ค๋ฅผ ํ์ฅํ๋ ์ฌ์ฉ์ ์ง์ ApiError ํด๋์ค๋ฅผ ์ ์ํฉ๋๋ค. ์ด๋ฅผ ํตํด ๊ด๋ จ ์ํ ์ฝ๋๋ฅผ ๊ฐ์ง ํน์ ์ค๋ฅ ํ์
์ ์์ฑํ ์ ์์ต๋๋ค. getUserFromDatabase ํจ์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฌ์ฉ์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์๋ฎฌ๋ ์ด์
ํ๋ฉฐ, ์ฌ์ฉ์๋ฅผ ์ฐพ์ ์ ์๋ ๊ฒฝ์ฐ ApiError๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค. handleGetUser ํจ์๋ getUserFromDatabase๊ฐ ๋ฐ์์ํจ ๋ชจ๋ ์ค๋ฅ๋ฅผ ํฌ์ฐฉํ๊ณ ํด๋ผ์ด์ธํธ์๊ฒ ์ ์ ํ ์๋ต์ ๋ฐํํฉ๋๋ค. ์ด ์ ๊ทผ ๋ฐฉ์์ ์ค๋ฅ๊ฐ ์ฐ์ํ๊ฒ ์ฒ๋ฆฌ๋๊ณ ์ ์ตํ ํผ๋๋ฐฑ์ด ์ ๊ณต๋๋๋ก ๋ณด์ฅํฉ๋๋ค.
ํ์ ์์ ํ API ์ํคํ ์ฒ ๊ตฌ์ถ
ํ์ ์์ ํ API ์ํคํ ์ฒ๋ฅผ ์ค๊ณํ๋ ๊ฒ์ ํ์ ์์ ์ฑ, ์ ์ง๋ณด์์ฑ ๋ฐ ํ์ฅ์ฑ์ ์ด์งํ๋ ๋ฐฉ์์ผ๋ก ์ฝ๋๋ฅผ ๊ตฌ์ฑํ๋ ๊ฒ์ ํฌํจํฉ๋๋ค. ๋ค์ ์ํคํ ์ฒ ํจํด์ ๊ณ ๋ คํ์ญ์์ค.
1. ๋ชจ๋ธ-๋ทฐ-์ปจํธ๋กค๋ฌ (MVC)
MVC๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ธ ๊ฐ์ง ๊ฐ๋ณ ๊ตฌ์ฑ ์์, ์ฆ ๋ชจ๋ธ(๋ฐ์ดํฐ), ๋ทฐ(์ฌ์ฉ์ ์ธํฐํ์ด์ค), ์ปจํธ๋กค๋ฌ(๋ ผ๋ฆฌ)๋ก ๋ถ๋ฆฌํ๋ ๊ณ ์ ์ ์ธ ์ํคํ ์ฒ ํจํด์ ๋๋ค. TypeScript API์์ ๋ชจ๋ธ์ ๋ฐ์ดํฐ ๊ตฌ์กฐ์ ํ์ ์ ๋ํ๋ด๊ณ , ๋ทฐ๋ API ์๋ํฌ์ธํธ์ ๋ฐ์ดํฐ ์ง๋ ฌํ๋ฅผ ๋ํ๋ด๋ฉฐ, ์ปจํธ๋กค๋ฌ๋ ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
2. ๋๋ฉ์ธ ์ฃผ๋ ์ค๊ณ (DDD)
DDD๋ ๋น์ฆ๋์ค ๋๋ฉ์ธ ์ฃผ๋ณ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ธ๋งํ๋ ๋ฐ ์ค์ ์ ๋ก๋๋ค. ์ฌ๊ธฐ์๋ ๋๋ฉ์ธ์ ํต์ฌ ๊ฐ๋ ์ ๋ํ๋ด๋ ์ํฐํฐ, ๊ฐ ๊ฐ์ฒด ๋ฐ ์ ๊ทธ๋ฆฌ๊ฒ์ดํธ๋ฅผ ์ ์ํ๋ ๊ฒ์ด ํฌํจ๋ฉ๋๋ค. TypeScript์ ํ์ ์์คํ ์ ํ๋ถํ๊ณ ํํ๋ ฅ์ด ๋ฐ์ด๋ ๋๋ฉ์ธ ๋ชจ๋ธ์ ์ ์ํ ์ ์์ผ๋ฏ๋ก DDD ์์น์ ๊ตฌํํ๋ ๋ฐ ์ ํฉํฉ๋๋ค.
3. ํด๋ฆฐ ์ํคํ ์ฒ
ํด๋ฆฐ ์ํคํ ์ฒ๋ ๊ด์ฌ์ฌ ๋ถ๋ฆฌ์ ํ๋ ์์ํฌ ๋ฐ ์ธ๋ถ ์ข ์์ฑ์ผ๋ก๋ถํฐ์ ๋ ๋ฆฝ์ฑ์ ๊ฐ์กฐํฉ๋๋ค. ์ฌ๊ธฐ์๋ ์ํฐํฐ ๊ณ์ธต(๋๋ฉ์ธ ๋ชจ๋ธ), ์ ์ค์ผ์ด์ค ๊ณ์ธต(๋น์ฆ๋์ค ๋ก์ง), ์ธํฐํ์ด์ค ์ด๋ํฐ ๊ณ์ธต(API ์๋ํฌ์ธํธ ๋ฐ ๋ฐ์ดํฐ ๋ณํ) ๋ฐ ํ๋ ์์ํฌ ๋ฐ ๋๋ผ์ด๋ฒ ๊ณ์ธต(์ธ๋ถ ์ข ์์ฑ)๊ณผ ๊ฐ์ ๊ณ์ธต์ ์ ์ํ๋ ๊ฒ์ด ํฌํจ๋ฉ๋๋ค. TypeScript์ ํ์ ์์คํ ์ ์ด๋ฌํ ๊ณ์ธต ๊ฐ์ ๊ฒฝ๊ณ๋ฅผ ์ ์ฉํ๊ณ ๋ฐ์ดํฐ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ํ๋ฅด๋๋ก ๋ณด์ฅํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
ํ์ ์์ ํ API์ ์ค์ ์์
TypeScript๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ์์ ํ API๋ฅผ ์ค๊ณํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ช ๊ฐ์ง ์ค์ ์์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
1. ์ ์์๊ฑฐ๋ API
์ ์์๊ฑฐ๋ API์๋ ์ ํ, ์ฃผ๋ฌธ, ์ฌ์ฉ์ ๋ฐ ๊ฒฐ์ ๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํ ์๋ํฌ์ธํธ๊ฐ ํฌํจ๋ ์ ์์ต๋๋ค. ์ด๋ฌํ ์ํฐํฐ์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๊ณ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ์ฌ์ฉํ์ฌ API๊ฐ ์์ ํ๋ ๋ฐ์ดํฐ๊ฐ ์ ํจํ์ง ํ์ธํจ์ผ๋ก์จ ํ์ ์์ ์ฑ์ ๊ฐํํ ์ ์์ต๋๋ค.
์์:
interface Product {
productId: string;
productName: string;
description: string;
price: number;
imageUrl: string;
category: string;
stockQuantity: number;
}
interface Order {
orderId: string;
userId: string;
items: { productId: string; quantity: number }[];
totalAmount: number;
shippingAddress: {
street: string;
city: string;
country: string;
};
orderStatus: OrderStatus;
createdAt: Date;
}
// API endpoint for creating a new product
async function createProduct(productData: Product): Promise<ApiResponse<Product>> {
// Validate product data
// Save product to database
// Return success response
return { success: true, data: productData };
}
2. ์์ ๋ฏธ๋์ด API
์์ ๋ฏธ๋์ด API์๋ ์ฌ์ฉ์, ๊ฒ์๋ฌผ, ๋๊ธ ๋ฐ ์ข์์๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ํ ์๋ํฌ์ธํธ๊ฐ ํฌํจ๋ ์ ์์ต๋๋ค. ์ด๋ฌํ ์ํฐํฐ์ ๋ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๊ณ enums๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ํ ์ ํ์ ์ฝํ ์ธ ๋ฅผ ๋ํ๋์ผ๋ก์จ ํ์ ์์ ์ฑ์ ๊ฐํํ ์ ์์ต๋๋ค.
์์:
interface User {
userId: string;
username: string;
fullName: string;
profilePictureUrl: string;
bio: string;
}
interface Post {
postId: string;
userId: string;
content: string;
createdAt: Date;
likes: number;
comments: Comment[];
}
interface Comment {
commentId: string;
userId: string;
postId: string;
content: string;
createdAt: Date;
}
// API endpoint for creating a new post
async function createPost(postData: Omit<Post, 'postId' | 'createdAt' | 'likes' | 'comments'>): Promise<ApiResponse<Post>> {
// Validate post data
// Save post to database
// Return success response
return { success: true, data: {...postData, postId: "unique-post-id", createdAt: new Date(), likes: 0, comments: []} as Post };
}
ํ์ ์์ ํ API ์ค๊ณ๋ฅผ ์ํ ๋ชจ๋ฒ ์ฌ๋ก
- TypeScript์ ๊ณ ๊ธ ํ์ ๊ธฐ๋ฅ ํ์ฉ: ๋งคํ๋ ํ์ , ์กฐ๊ฑด๋ถ ํ์ , ์ ํธ๋ฆฌํฐ ํ์ ๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ํ์ฉํ์ฌ ๋ณด๋ค ํํ๋ ฅ ์๊ณ ์ ์ฐํ ํ์ ์ ์๋ฅผ ์์ฑํฉ๋๋ค.
- ๋จ์ ํ ์คํธ ์์ฑ: API ์๋ํฌ์ธํธ์ ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ ๋ก์ง์ด ์์๋๋ก ์๋ํ๋์ง ์ฒ ์ ํ ํ ์คํธํฉ๋๋ค.
- ๋ฆฐํ ๋ฐ ํฌ๋งทํ ๋๊ตฌ ์ฌ์ฉ: ESLint ๋ฐ Prettier์ ๊ฐ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ ์ผ๊ด๋ ์ฝ๋ฉ ์คํ์ผ๊ณผ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์ ์ฉํฉ๋๋ค.
- API ๋ฌธ์ํ: API ์๋ํฌ์ธํธ, ๋ฐ์ดํฐ ๊ตฌ์กฐ ๋ฐ ์ค๋ฅ ์ฒ๋ฆฌ์ ๋ํ ๋ช ํํ๊ณ ํฌ๊ด์ ์ธ ๋ฌธ์๋ฅผ ์ ๊ณตํฉ๋๋ค. Swagger์ ๊ฐ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ์ฌ TypeScript ์ฝ๋์์ API ๋ฌธ์๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
- API ๋ฒ์ ๊ด๋ฆฌ ๊ณ ๋ ค: ๋ฒ์ ๊ด๋ฆฌ ์ ๋ต์ ๊ตฌํํ์ฌ API์ ํฅํ ๋ณ๊ฒฝ ์ฌํญ์ ๋๋นํฉ๋๋ค.
๊ฒฐ๋ก
TypeScript๋ฅผ ์ฌ์ฉํ ํ์ ์์ ํ API ์ค๊ณ๋ ๊ฒฌ๊ณ ํ๊ณ ์ ์ง๋ณด์ ๊ฐ๋ฅํ๋ฉฐ ํ์ฅ ๊ฐ๋ฅํ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ถํ๊ธฐ ์ํ ๊ฐ๋ ฅํ ์ ๊ทผ ๋ฐฉ์์ ๋๋ค. ๋ช ํํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๊ณ , ๋ฐ์ดํฐ ์ ํจ์ฑ ๊ฒ์ฌ๋ฅผ ๊ตฌํํ๋ฉฐ, ์ค๋ฅ๋ฅผ ์ฐ์ํ๊ฒ ์ฒ๋ฆฌํจ์ผ๋ก์จ ๋ฐํ์ ์ค๋ฅ๋ฅผ ํฌ๊ฒ ์ค์ด๊ณ ๊ฐ๋ฐ์ ์์ฐ์ฑ์ ํฅ์์ํค๋ฉฐ ์ฝ๋์ ์ ๋ฐ์ ์ธ ํ์ง์ ๋์ผ ์ ์์ต๋๋ค. ์ด ๊ฐ์ด๋์ ์ค๋ช ๋ ์์น๊ณผ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์์ฉํ์ฌ ํ๋ ์ํํธ์จ์ด ๊ฐ๋ฐ์ ์๊ตฌ ์ฌํญ์ ์ถฉ์กฑํ๋ ํ์ ์์ ํ API๋ฅผ ๋ง๋์ญ์์ค.